package com.xcw.bos.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.xcw.bos.domain.Region;
import com.xcw.bos.domain.Subarea;
import com.xcw.bos.utils.FileUtils;
import com.xcw.bos.web.action.base.BaseAction;

/**
 * 分区操作Action
 * @author xcw
 *
 */
@Controller
@Scope("prototype")
public class SubareaAction extends BaseAction<Subarea>{

	//添加分区方法
	public String save(){
		subareaService.save(model);
		return "subareaList";
	}
	
	//分页查询
	public String pageQuery(){
		String addresskey = model.getAddresskey();
		Region region = model.getRegion();
		
		DetachedCriteria dc = pageBean.getDetachedCriteria();
		if(StringUtils.isNotBlank(addresskey)){
			dc.add(Restrictions.like("addresskey", "%" + addresskey + "%"));
		}
		
		if(region != null){
			String province = region.getProvince();
			String city = region.getCity();
			String district = region.getDistrict();
			
			DetachedCriteria regionDc = dc.createCriteria("region");
			if(StringUtils.isNotBlank(province)){
				regionDc.add(Restrictions.like("province", "%" + province + "%"));
			}
			
			if(StringUtils.isNotBlank(city)){
				regionDc.add(Restrictions.like("city", "%" + city + "%"));
			}
			
			if(StringUtils.isNotBlank(district)){
				regionDc.add(Restrictions.like("district", "%" + district + "%"));
			}
		}
		subareaService.pageQuery(pageBean);
		String[] excudes = new String[]{"page", "pageSize",
				"detachedCriteria", "subareas", "decidedZone" };
		this.writePageBean2Json(excudes, pageBean);
		return NONE;
	}
	
	//导出分区数据到Excel中， 提供下载
	public String exportXls() throws IOException{
		List<Subarea> list = subareaService.findAll();
		//创建一个Excel表格
		HSSFWorkbook workbook = new HSSFWorkbook();
		//创建一个sheet页
		HSSFSheet sheet = workbook.createSheet("分区数据");
		HSSFRow headRow = sheet.createRow(0);
		headRow.createCell(0).setCellValue("分区编号");
		headRow.createCell(1).setCellValue("地址关键字");
		headRow.createCell(2).setCellValue("省");
		headRow.createCell(3).setCellValue("市");
		headRow.createCell(4).setCellValue("区");
		
		if(list != null && list.size() > 0){
			for(Subarea subarea : list){
				String addresskey = subarea.getAddresskey();
				String province = subarea.getRegion().getProvince();
				String city = subarea.getRegion().getCity();
				String district = subarea.getRegion().getDistrict();
				HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			    dataRow.createCell(0).setCellValue(subarea.getId());
			    dataRow.createCell(1).setCellValue(addresskey);
				dataRow.createCell(2).setCellValue(province);
				dataRow.createCell(3).setCellValue(city);
				dataRow.createCell(4).setCellValue(district);
		    }
	    }	
			
			
		String fileName = "分区数据.xls";
		fileName = FileUtils.encodeDownloadFilename(fileName, 
				ServletActionContext.getRequest().getHeader("user-agent"));
		ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
		ServletActionContext.getResponse().setContentType(ServletActionContext.getServletContext().getMimeType(fileName));
		ServletActionContext.getResponse().setHeader("content-disposition",
				"attachment;filename=" + fileName);
		workbook.write(out);
		return NONE;
		
	}
	
	//查询未关联到定区的分区
	public String findnoassosiaction(){
		DetachedCriteria dc = DetachedCriteria.forClass(Subarea.class);
		dc.add(Restrictions.isNull("decidedZone"));
		List<Subarea> list = subareaService.findByCondition(dc);
		
		String[] excluds = new String[]{"decidedZone","region"};
		this.writeList2Json(excluds, list);
		return NONE;
	}
    	
}
